package cn.com.template.controller.system.admin;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.commons.Paginator;
import cn.com.commons.utils.StringUtil;
import cn.com.dal.dataobject.hr.DeptDO;
import cn.com.dal.dataobject.sys.DictDO;
import cn.com.service.hr.DeptService;
import cn.com.service.sys.DictService;
import cn.com.support.context.UserContext;
import cn.com.support.message.MessageResult;
import cn.com.template.ui.Flexigrid;
import cn.com.template.utils.FlexigridUtil;

/**
 * 系统字典管理
 * @author Zoe
 *
 */
@Controller
@RequestMapping("/system/admin/dictmanager")
public class DictionaryController {

	@Autowired
	private DictService dictService;
	@Autowired
	private DeptService deptService;
	
	@RequestMapping
	public ModelAndView forwardDictManager(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("system/admin/dictmanager");
		List<DeptDO> deptLst = deptService.selectList(null);
		request.setAttribute("deptLst", deptLst);
		return mav;
	}
	
	
	@RequestMapping("/getDictInfo")
	@ResponseBody
	public Flexigrid getDictInfo(HttpServletRequest request, HttpServletResponse response){
		Flexigrid flexigrid = null;
		try {
			Paginator<DictDO, List<DictDO>> paginator = new Paginator<DictDO, List<DictDO>>();
			//deptname
			String name = request.getParameter("dictname");
			DictDO dictDO = new DictDO();
			//dictDO.setAttribute(null);
			if(StringUtil.isNotBlank(name)){
				dictDO.setName(name.trim());
			}
			paginator.setCondition(dictDO);
			// 设置分页器
			paginator.setPageSize(Integer.parseInt(request.getParameter("rp")));
			paginator.setCurrentPage(Integer.parseInt(request.getParameter("page")));
			dictService.selectByPage(paginator);
			//PACKAGE FLEXIGRID DATA
			flexigrid = FlexigridUtil.formatFlexigridData(paginator);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flexigrid;
	}

	@RequestMapping("/addDictInfo")
	@ResponseBody
	public MessageResult<Long> addDictInfo(HttpServletRequest request, HttpServletResponse response){
		MessageResult<Long> result = null;
		try {
			String dictName = request.getParameter("dictNameText");
			String dictDesc = request.getParameter("dictdesc");
			String module = request.getParameter("modual");
			String code = request.getParameter("code");
			DictDO dictDO = new DictDO();
			dictDO.setName(dictName.trim());
			dictDO.setModule(module.trim());
			dictDO.setCode(code);
			dictDO.setSort((byte)0);
			dictDO.setDescription(dictDesc.trim());
			dictDO.setCreator(UserContext.getUsername());
			result = dictService.insert(dictDO);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@RequestMapping("/updateDictInfo")
	@ResponseBody
	public MessageResult<Integer> updateDictInfo(HttpServletRequest request, HttpServletResponse response){
		MessageResult<Integer> result = null;
		try {
			String dictName = request.getParameter("dictNameText");
			String dictDesc = request.getParameter("dictdesc");
			String module = request.getParameter("modual");
			String code = request.getParameter("code");
			String id = request.getParameter("dictid");
			DictDO dictDO = new DictDO();
			dictDO.setId(Long.parseLong(id.trim()));
			dictDO.setName(dictName.trim());
			dictDO.setDescription(dictDesc.trim());
			dictDO.setModule(module.trim());
			dictDO.setCode(code);
			dictDO.setModifier(UserContext.getUsername());
			result = dictService.updateById(dictDO);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@RequestMapping("/delDictInfo")
	@ResponseBody
	public MessageResult<Integer> delDictInfo(HttpServletRequest request, HttpServletResponse response){
		MessageResult<Integer> result = null;
		try {
			String idStrs = request.getParameter("ids");
			result = dictService.deleteById(Arrays.asList(idStrs.split(",")));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

}
